﻿using UnityEngine;


// AssetPath : 相对Asset文件的相对路径， 可能带有后缀,如："MyTextures/hello.png"
// FullPath  : 全路径,可能带有后缀
// ProjectPath:相对工程资源的路径,比如："Assets/MyTextures/hello.png"

class AssetBundleUtil
{
    public static string Normarlize(string s)
    {
        return s.Replace("\\", "/");
    }

    /// <summary>
    /// 获得Bundle Name
    /// </summary>
    /// <param name="assetPath"> assetPath 或者ProjectPath </param>
    /// <returns></returns>
    public static string GetBundleName(string assetOrProjectPath)
    {
        if (string.IsNullOrEmpty(assetOrProjectPath))
        {
            Debug.LogError("AssetBundleUtil.GetBundleName: Input is null");
            return null;
        }

        string assetPath = assetOrProjectPath.ToLower();
        if (assetOrProjectPath.Contains("Assets"))
        {
            assetPath = assetOrProjectPath.Substring("Assets".Length + 1);
        }

        string bundleName = assetPath.Replace("/", "_");
        if (bundleName.LastIndexOf(".") >= 0)
        {
            bundleName = bundleName.Substring(0, bundleName.LastIndexOf("."));
        }

        return bundleName + AssetBundleConfig.ConstAssetTail;
    }
    
    /// <summary>
    /// 
    /// </summary>
    /// <param name="assetPath"></param>
    /// <returns></returns>
    public static string AssetPathToFullPath(string assetOrProjectPath)
    {
        string assetPath = assetOrProjectPath;
        if (assetOrProjectPath.Contains("Assets"))
        {
            assetPath = assetOrProjectPath.Substring("Assets".Length + 1);
        }
        return Application.dataPath + "/" + assetPath;
    }

    public static string FullPathToAssetPath(string fullPath)
    {
        string assetPath = fullPath.Substring(Application.dataPath.Length);
        if (assetPath.Contains("Assets"))
        {
            assetPath = assetPath.Substring("Assets".Length + 1);
        }

        return  assetPath;
    }
}